﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace MultiIpLook
{
    public partial class Form1 : Form
    {
        #region Attributes

        private string QQWryFile = Application.StartupPath + @"\QQWry.Dat";

        private delegate string[] mygetRichTextBox();

        int index = 0;
        int maxthread = 50;

        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            this.progressBar1.Maximum = this.rbtnIP.Lines.Length;
            this.progressBar1.Value = 0;

            ThreadPool.SetMaxThreads(50, 50);
            for(int i=0;i<50;i++) ThreadPool.QueueUserWorkItem(new WaitCallback(this.thread));



            //Thread myThread = new Thread(new ThreadStart(thread));
            //myThread.Start();
        }

        string result = "";
        private void thread(object obj)
        {
            try
            {
                // System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
                //Regex regex = new Regex(@"^[0-2]{0,1}[0-9]{1,2}(\.[0-2]{0,1}[0-9]{1,2}){3}$");
                //if (!regex.IsMatch(this.tbip.Text))
                //{
                //    MessageBox.Show("请输入合法的IP地址");
                //    return;
                //}

                //if (!CheckQQWryDatPath()) return;

                //IPLocation loc = new IPLocation();

               // QQWryHelper QQWry = new QQWryHelper(QQWryFile);
                string ip = GetIP();
                while (ip != "")
                {
                    //loc = QQWry.GetIPLocation(ip);
                    result += ip + "\n"; //loc.ip + "\t" + loc.area + "\t" + loc.country + "\n";
                    this.progressBar1.Value++; //= (index - 1);
                    ip = GetIP();
                }

                //foreach (string ip in getRichTextBox())
                //{
                //    /loc = QQWry.GetIPLocation(ip);
                //    result += ip + "\n"; //loc.ip + "\t" + loc.area + "\t" + loc.country + "\n";
                //    this.progressBar1.Value++;
                //}
                if (isComplete() == 0)
                {
                    this.rbtnResult.Text = result;
                }
                //this.rbtnResult.AppendText(watch.Elapsed.ToString());
                //watch.Stop();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private string[] getRichTextBox()
        {
            if (this.InvokeRequired)
            {
                return this.Invoke(new mygetRichTextBox(getRichTextBox)) as string[];
            }
            else
            {
                return this.rbtnIP.Lines;
            }
        }

        private string GetIP()
        {
            string ip = "";
            lock (this)
            {
                if (index < this.rbtnIP.Lines.Length)
                {
                    ip = this.rbtnIP.Lines[index];
                    index++;
                }
            }
            return ip;
        }

        private int isComplete()
        {
            if (index == this.rbtnIP.Lines.Length)
            {
                maxthread--;
                int av = 0;
                int max = 0;
                ThreadPool.GetAvailableThreads(out max, out av);
                if ( maxthread== 0||max==49)
                {
                    return 0;
                }
            }
            return 1;
        }
    }
}
